
package edu.ccut.saturn.tag.complex;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import edu.ccut.saturn.tag.AbstractComplexTag;
import edu.ccut.saturn.tag.complex.util.MenuHelper;
import edu.ccut.saturn.tag.complex.util.MenuItemVo;
import edu.ccut.saturn.tag.complex.util.StringHelper;
import edu.ccut.saturn.tag.complex.util.XmlHelper;


public class SaturnMenuBar extends AbstractComplexTag {

	private static final long serialVersionUID = -1828508017410250603L;

	private static final String MENUBAR_TAG_KEY = "menubar_tag";

	public static final String TEMPLATE_MENUBAR_FTL = "template/menubar.ftl";

	private String config;

	private String menuId = "";
	private String fitWidth = "";
	private String target = "";
	private String showBorder = "";

	private String itemIdKey;
	private String itemParentIdKey;
	private String itemNameKey;
	private String itemLabelKey;
	private String itemIconKey;
	private String itemPathKey;
	private String itemDisableIconKey;
	private String itemVisibleKey;
	private String itemCommandKey;

	private Object value;

	public SaturnMenuBar() {
		super(MENUBAR_TAG_KEY);
	}

	@Override
	public int doStartTag() throws JspException {
		JspWriter out = super.getWriter();
		List<MenuItemVo> rootMenuItemList = this.getRootMenuItemVoList();

		try {

			out.print("<xml");
			out.print(XmlHelper.tagProperty1("id", getMenuId()));
			out.print(">\n");
			out.print("<items>\n");
			try {

				outputMenuItems(this.getWriter(), rootMenuItemList);

			} catch (Exception e1) {

				e1.printStackTrace();
			}
			out.print("</items>\n");
			out.print("</xml>\n");

//			out.print("<table id=\"" + getId() + "\" ");
//			String s1;
//			if (StringHelper.isNotEmpty(s1 = TagProperty.stylePlacement(this)))
//				out.print(" style=\"" + s1 + "\"");
//
//			out.print(">");
//
//			out.print("</table>\n");
//
			//this.writer("<script language=\"javascript\">");
//			this.writer("new PraseItem();");
//			this.writer("var __SKIN_PATH =\""
//					+ this.getRequest().getContextPath() + "/images\";");
//			this.writer("</script>");
//			this.writer(TEMPLATE_MENUBAR_FTL, this);

		} catch (Exception e) {

			e.printStackTrace();
		}

		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	protected void outputMenuItems(Writer writer,
			List<MenuItemVo> rootMenuItemList) throws Exception {

		if (rootMenuItemList != null) {
			for (MenuItemVo rootMenuItemVo : rootMenuItemList) {
				MenuHelper.outputStartSection1(writer, rootMenuItemVo);
				List<MenuItemVo> childrenmenuItemList = this
						.getChildrenMenuItemVoList(rootMenuItemVo.getId());
				if (childrenmenuItemList != null
						&& !childrenmenuItemList.isEmpty()) {
					outputMenuItems(writer, childrenmenuItemList);
				}

				MenuHelper.outputEndSection1(writer, rootMenuItemVo);
			}
		}
	}

	@SuppressWarnings("unchecked")
	private List<MenuItemVo> getRootMenuItemVoList() {
		List<MenuItemVo> rootMenuItemList = new ArrayList<MenuItemVo>();

		if (this.value != null && (this.value instanceof List)) {
			List<Map<String, String>> menus = (List<Map<String, String>>) this.value;

			for (Map<String, String> menuItem : menus) {
				if (StringHelper.isEmpty(menuItem
						.get(this.getItemParentIdKey()))) {

					rootMenuItemList.add(createMenuItem(menuItem));
				}
			}
		}

		return rootMenuItemList;
	}

	@SuppressWarnings("unchecked")
	private List<MenuItemVo> getChildrenMenuItemVoList(String parentId) {
		List<MenuItemVo> itemList = new ArrayList<MenuItemVo>();

		if (this.value != null && (this.value instanceof List)) {
			List<Map<String, String>> menus = (List<Map<String, String>>) this.value;

			for (Map<String, String> menuItem : menus) {
				String parentIdKey = this.getItemParentIdKey();
				if (!StringHelper.isEmpty(menuItem.get(parentIdKey))
						&& menuItem.get(parentIdKey).equals(parentId)) {

					itemList.add(createMenuItem(menuItem));
				}
			}
		}

		return itemList;
	}

	private MenuItemVo createMenuItem(Map<String, String> menuItem) {
		MenuItemVo menuItemVo = new MenuItemVo();

		menuItemVo.setId(menuItem.get(this.getItemIdKey()));
		menuItemVo.setParentId(menuItem.get(this.getItemParentIdKey()));
		menuItemVo.setName(StringHelper.getNotEmptyValue(menuItem.get(this
				.getItemNameKey()), "name"));
		menuItemVo.setLabel(StringHelper.getNotEmptyValue(menuItem.get(this
				.getItemLabelKey()), "空"));

		String orginalPath = menuItem.get(this.getItemPathKey());
		String path = null;
		if (!StringHelper.isEmpty(orginalPath)) {
			if (orginalPath.startsWith("/")) {
				path = super.getRequest().getContextPath() + orginalPath;
			} else {
				path = orginalPath;
			}
		}
		menuItemVo.setPath(path);
		// TODO:WEB 找个图片更新
		menuItemVo.setIcon(StringHelper.getNotEmptyValue(menuItem.get(this
				.getItemIconKey()), super.getRequest().getContextPath()
				+ "/images/003.gif"));
		menuItemVo.setDisableIcon(StringHelper.getNotEmptyValue(menuItem
				.get(this.getItemDisableIconKey()), super.getRequest()
				.getContextPath()
				+ "/images/003.gif"));
		return menuItemVo;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public String getConfig() {
		return config;
	}

	public void setConfig(String config) {
		this.config = config;
	}

	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public String getFitWidth() {
		return fitWidth;
	}

	public void setFitWidth(String fitWidth) {
		this.fitWidth = fitWidth;
	}

	public String getMenu() {
		return this.getMenuId();
	}

	public String getShowBorder() {
		return showBorder;
	}

	public void setShowBorder(String showBorder) {
		this.showBorder = showBorder;
	}

	public String getItemIdKey() {
		return itemIdKey;
	}

	public void setItemIdKey(String itemIdKey) {
		this.itemIdKey = itemIdKey;
	}

	public String getItemParentIdKey() {
		return itemParentIdKey;
	}

	public void setItemParentIdKey(String itemParentIdKey) {
		this.itemParentIdKey = itemParentIdKey;
	}

	public String getItemNameKey() {
		return itemNameKey;
	}

	public void setItemNameKey(String itemNameKey) {
		this.itemNameKey = itemNameKey;
	}

	public String getItemLabelKey() {
		return itemLabelKey;
	}

	public void setItemLabelKey(String itemLabelKey) {
		this.itemLabelKey = itemLabelKey;
	}

	public String getItemIconKey() {
		return itemIconKey;
	}

	public void setItemIconKey(String itemIconKey) {
		this.itemIconKey = itemIconKey;
	}

	public String getItemPathKey() {
		return itemPathKey;
	}

	public void setItemPathKey(String itemPathKey) {
		this.itemPathKey = itemPathKey;
	}

	public String getItemDisableIconKey() {
		return itemDisableIconKey;
	}

	public void setItemDisableIconKey(String itemDisableIconKey) {
		this.itemDisableIconKey = itemDisableIconKey;
	}

	public String getItemVisibleKey() {
		return itemVisibleKey;
	}

	public void setItemVisibleKey(String itemVisibleKey) {
		this.itemVisibleKey = itemVisibleKey;
	}

	public String getItemCommandKey() {
		return itemCommandKey;
	}

	public void setItemCommandKey(String itemCommandKey) {
		this.itemCommandKey = itemCommandKey;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		if (value instanceof String) {
			this.value = super.getExpressValueObject((String) value);
		} else {
			this.value = value;
		}
	}
}
